package wsp.dailymarket.deals;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author Matt
 */
public class DealCode {

	public static String generateCode(String userid, String companyname, int dealid, int uniqueid, long timeNow) {
		try {
			String fromString = userid + ":" + companyname + ":" + dealid + ":" + timeNow + ":" + uniqueid;
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.reset();
			byte[] hash = md.digest(fromString.getBytes("UTF-8"));
			BigInteger big = new BigInteger(1, hash);
			String toHash = big.toString(16);
			while (toHash.length() < 32) {
				toHash = "0" + toHash;
			}
			return toHash;
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace(System.err);
		} catch (UnsupportedEncodingException ex) {
			ex.printStackTrace(System.err);
		}
		return "";
	}
}
